﻿using System;
using System.Collections.Generic;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Implements;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Biz
{
    public class ChuyenNganhBiz
    {
        #region Properties

        private IChuyenNganhDao _chuyenNganhDao;
        private IChuyenNganhDao ChuyenNganhData
        {
            get { return _chuyenNganhDao ?? (_chuyenNganhDao = new ChuyenNganhDao()); }
        }

        #endregion

        #region Constructors

        #endregion

        #region Methods

        public List<ChuyenNganh> GetAllChuyenNganhs()
        {
            return ChuyenNganhData.GetAll();
        }

        public object GetChuyenNganhs(int startIndex, int pageSize, string sorting, string name)
        {
            try
            {
                int totalRecords;
                var chuyenNganhs = ChuyenNganhData.GetChuyenNganhs(startIndex, pageSize, sorting, name, out totalRecords);
                return new { Result = "OK", Records = chuyenNganhs, TotalRecordCount = totalRecords };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public ChuyenNganh CreateChuyenNganh(ChuyenNganh chuyenNganh)
        {
            // validate inputs
            if (chuyenNganh == null) return null;

            return string.IsNullOrEmpty(chuyenNganh.Ten) ? null : ChuyenNganhData.AddChuyenNganh(chuyenNganh);
        }

        public ChuyenNganh GetById(int id)
        {
            return id > 0 ? ChuyenNganhData.GetById(id) : null;
        }

        public void UpdateChuyenNganh(ChuyenNganh chuyenNganh)
        {
            if (chuyenNganh == null) return;

            ChuyenNganhData.UpdateChuyenNganh(chuyenNganh);
        }

        public object DeleteChuyenNganh(int id)
        {
            try
            {
                ChuyenNganhData.DeleteChuyenNganh(id);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion
    }
}
